<?php

namespace app\api\controller;

use app\api\library\WechatPay;
use app\common\model\NewVoucher;
use think\Log;

class NewOrder extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * Name 优惠券领取
     */
    public function send()
    {
        $order_no = $this->request->post('order_no');

        $order = \app\common\model\NewOrder::where('order_no',$order_no)->find();
        //Log::record($order_no,'领取');
       /* if($order->admin_id == 35 || $order->admin_id == 38){*/
            
            $orders = \app\common\model\NewOrder::where('order_no',$order_no)->where('status',1)->select();
            if(empty($orders)){
                $this->error('未查询到待领取的优惠券');
            }
            $total_money = 0;
            foreach ($orders as $order){
                $order->status = 0;
                $order->user_id = $this->auth->id;
                $res = $order->save();
                if(!$res) $this->error('领取失败');
                $total_money += $order->money;
                $voucher = NewVoucher::where('id',$order->voucher_id)->find();
                $order_no = 'new'.time().rand(1000,9999);
                $wechat_obj = new WechatPay(2);
                
                list($code,$data) = $wechat_obj->sendCoupon($voucher->stock_id,$order_no,$this->auth->openid);
                Log::record($code,'领取结果');
                Log::record($data,'领取结果');
                if($code == 1)
                {
                    $order->status = 2;
                    $order->voucher_stock_id = $voucher->stock_id;
                    $order->user_id = $this->auth->id;
                    $order->user_coupon_id = $data['coupon_id'];
                    $order->save();
                    $voucher->sy_number -= $order->number;
                    $voucher->save();
                }/*else{
                    $order->status = 0;
                    $order->user_id = $this->auth->id;
                    $order->save();
                }*/
            }
            $this->success('成功',$total_money);


       /* }else{
            $this->error('领取失败');*/
            /*if(!$order) $this->error('参数错误，请重新扫描二维码');
            if($order->status != 1) $this->error('优惠券已领取,请勿重复领取');

            Db::startTrans();
            $voucher = NewVoucher::where('id',$order->voucher_id)->lock(true)->find();
            if(!$voucher) $this->error('参数错误，请重新扫描二维码');
            if($voucher->sy_number < 0) $this->error('优惠券已抢完');

            //浚县农商行
            if($voucher->admin_id == 31)
            {
                $count = \app\common\model\NewOrder::where('admin_id',$voucher->admin_id)->where('status','>',1)->where('user_phone',$order->user_phone)->count();
                if($count > 0){
                    $this->error('用户已领取过优惠券了');
                }
                $order_no = 'new'.time().rand(1000,9999);
                $wechat_obj = new WechatPay(2);
                list($code,$data) = $wechat_obj->sendCoupon($voucher->stock_id,$order_no,$this->auth->openid);
                Log::record($code,'领取结果');
                Log::record($data,'领取结果');
            }else{
                for ($i = 1;$i<= $order->number;$i++){
                    $order_no = 'new'.time().rand(1000,9999);
                    $wechat_obj = new WechatPay(2);
                    list($code,$data) = $wechat_obj->sendCoupon($voucher->stock_id,$order_no,$this->auth->openid);
                    Log::record($code,'领取结果');
                    Log::record($data,'领取结果');
                }
            }



            if($code == 1)
            {
                try {
                    $order->status = 2;
                    $order->voucher_stock_id = $voucher->stock_id;
                    $order->user_id = $this->auth->id;
                    $order->user_coupon_id = $data['coupon_id'];
                    $order->save();

                    $voucher->sy_number -= $order->number;
                    $voucher->save();
                    DB::commit();
                    $hui = bcmul($voucher->money_hui,$order->number);
                    $this->success('成功',$hui);
                }catch (Exception $e){
                    Db::rollback();
                    $this->error('领取失败');
                }

            }else{
                Db::rollback();
                $this->error('领取失败');
            }*/
       /* }*/
    }


}
